//
//  CIBNActorCell.m
//  CIBN
//
//  Created by ZNGOO on 16/7/20.
//  Copyright © 2016年 韦儒健. All rights reserved.
//

#import "CIBNActorCell.h"

#define Item_W ((ScreenWidth - 50)/5)

@interface CIBNActorCell()

@property (nonatomic,strong) UIScrollView * scrollView;
@end

@implementation CIBNActorCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
    
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.scrollView];
        
    }
    
    return self;
}

- (void)setCellsModel:(CIBNPlayHomeModel *)model{
   
    
    // 刷新时先移除ScrollView上的子控件
    if (self.scrollView.subviews.count > 0) {
        for (UIView * view in self.scrollView.subviews) {
            [view removeFromSuperview];
        }
    }
    
    
    NSMutableString * actor = [[model.actor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy];
    NSMutableString * director = [[model.director stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy];
    
    if (actor.length > 0) {
        [actor deleteCharactersInRange:NSMakeRange(actor.length - 1, 1)];
    }
    
    if (director.length > 0) {
        [director deleteCharactersInRange:NSMakeRange(director.length - 1, 1)];
    }
    
    
    
//     NSLog(@"%@--%@",actor, director);
    
    NSMutableArray * actorArray = [[actor componentsSeparatedByString:@";"] mutableCopy];
    NSMutableArray * directorArray = [[director componentsSeparatedByString:@";"] mutableCopy];
    
    for (NSString * name in directorArray) {
        [actorArray addObject:name];
    }
    
    [actorArray removeObject:@""];
    
    NSLog(@"%@",actorArray);
    self.scrollView.contentSize = CGSizeMake((Item_W + 10) * actorArray.count, Item_W + 20);
    for (int index = 0; index < actorArray.count; index++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake((Item_W + 10) * index + 10, 0, Item_W, Item_W)];
        imageView.backgroundColor = [UIColor grayColor];
        imageView.layer.cornerRadius = Item_W/2;
        imageView.clipsToBounds = YES;
        [self.scrollView addSubview:imageView];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake((Item_W + 10) * index + 10, CGRectGetMaxY(imageView.frame), Item_W, 20)];
        name.text = actorArray[index];
        name.textAlignment = NSTextAlignmentCenter;
        name.textColor = [UIColor grayColor];
        name.font = [UIFont systemFontOfSize:12];
        [self.scrollView addSubview:name];
    }

}
@end
